package com.csit.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csit.dao.BuyDao;
import com.csit.dao.BuyDetailDao;
import com.csit.dao.ProductDao;
import com.csit.entity.Buy;
import com.csit.entity.BuyDetail;
import com.csit.entity.DataDictionary;
import com.csit.entity.Product;
import com.csit.service.BuyService;
import com.csit.util.DateUtils;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.csit.util.TimeUtil;

@Service
public class BuyServiceImpl extends BaseServiceImpl<Buy, Integer> implements BuyService {
	@Resource
	private BuyDao buyDao;
	@Resource
	private BuyDetailDao buyDetailDao;
	@Resource
	private ProductDao productDao;
	@Resource
	public void setBaseDao(BuyDao buyDao) {
		super.setBaseDao(buyDao);
	}
	
	public ResponseMsgUtil addBuy(Buy buy,String productids,String qtys,String prices,String notes) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(productids)){
			msgUtil.setMsg("至少采购一种商品");
			return msgUtil;
		}
		if(buy.getSupplier()==null){
			msgUtil.setMsg("供应商不能为空");
			return msgUtil;
		}
		if(buy.getBuyDate()==null){
			msgUtil.setMsg("入库日期不能为空");
			return msgUtil;
		}
		if(buy.getStoreHouse()==null){
			msgUtil.setMsg("仓库不能为空");
			return msgUtil;
		}
		String[] productidArray = productids.split(SPLIT_REGEX);
		String[] qtyArray = qtys.split(SPLIT_REGEX);
		String[] priceArray = prices.split(SPLIT_REGEX);
		String[] noteArray = notes.split(SPLIT_REGEX);
		
		buy.setCreateDate(new Date());
		saveEntity(buy);
		for (int i=0;i<productidArray.length;i++) {
			BuyDetail buyDetail = new BuyDetail();
			
			Product product = productDao.get(Integer.parseInt(productidArray[i]));
			
			buyDetail.setBuy(buy);
			buyDetail.setProduct(product);
			buyDetail.setQuantity(Double.parseDouble(qtyArray[i]));
			
			//更新库存
			product.setCurrentQuantity(product.getCurrentQuantity()+buyDetail.getQuantity());
			
			buyDetail.setPrice(Double.parseDouble(priceArray[i]));
			buyDetail.setAmount(buyDetail.getQuantity()*buyDetail.getPrice());
			buyDetail.setPosition(i+1);
			if(!"*".equals(noteArray[i])){
				buyDetail.setNote(noteArray[i].trim());
			}
			buyDetailDao.save(buyDetail);
		}
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public String getCode(DataDictionary shop) {
		Long count = buyDao.getCount(shop);
		String code = "B"+TimeUtil.formatDate(new Date(),"yyyyMMdd").substring(2)+String.format("%03d", count+1);
		return code;
	}

	public ResponseMsgUtil updateBuy(Buy buy, String ids,String productids, String qtys,String prices, String notes, String deleteids) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(productids)){
			msgUtil.setMsg("至少采购一种商品");
			return msgUtil;
		}
		if(buy.getSupplier()==null){
			msgUtil.setMsg("供应商不能为空");
			return msgUtil;
		}
		if(buy.getBuyDate()==null){
			msgUtil.setMsg("入库日期不能为空");
			return msgUtil;
		}
		if(buy.getStoreHouse()==null){
			msgUtil.setMsg("仓库不能为空");
			return msgUtil;
		}
		buyDao.update(buy);
		String[] idArray = ids.split(SPLIT_REGEX);
		String[] productidArray = productids.split(SPLIT_REGEX);
		String[] qtyArray = qtys.split(SPLIT_REGEX);
		String[] priceArray = prices.split(SPLIT_REGEX);
		String[] noteArray = notes.split(SPLIT_REGEX);
		
		int index = buyDetailDao.getMaxPostion(buy);
		//先做新增和修改
		for(int i=0;i<idArray.length;i++){
			Product product = productDao.get(Integer.parseInt(productidArray[i]));
			//更新
			if(Integer.parseInt(idArray[i])!=0){
				BuyDetail buyDetail = buyDetailDao.get(Integer.parseInt(idArray[i]));
				//如果物品换掉
				if(buyDetail.getProduct().getProductID()!=product.getProductID()){
					buyDetail.setProduct(product);
					Product oldProduct = buyDetail.getProduct();
					//先更新之前的
					oldProduct.setCurrentQuantity(oldProduct.getCurrentQuantity()-buyDetail.getQuantity());
					//然后更新修改之后的
					product.setCurrentQuantity(product.getCurrentQuantity()+Double.parseDouble(qtyArray[i]));
				}else{
					//更新库存
					product.setCurrentQuantity(product.getCurrentQuantity()-buyDetail.getQuantity()+Double.parseDouble(qtyArray[i]));
				}
				buyDetail.setProduct(product);
				buyDetail.setQuantity(Double.parseDouble(qtyArray[i]));
				buyDetail.setPrice(Double.parseDouble(priceArray[i]));
				buyDetail.setAmount(buyDetail.getQuantity()*buyDetail.getPrice());
				if(!"*".equals(noteArray[i])){
					buyDetail.setNote(noteArray[i].trim());
				}
				//buyDetailDao.update(buyDetail);
			//新增
			}else{
				BuyDetail buyDetail = new BuyDetail();
				
				buyDetail.setBuy(buy);
				buyDetail.setBuyDetailID(Integer.parseInt(idArray[i]));
				
				buyDetail.setProduct(product);
				buyDetail.setQuantity(Double.parseDouble(qtyArray[i]));
				buyDetail.setPrice(Double.parseDouble(priceArray[i]));
				buyDetail.setAmount(buyDetail.getQuantity()*buyDetail.getPrice());
				if(!"*".equals(noteArray[i])){
					buyDetail.setNote(noteArray[i].trim());
				}
				buyDetail.setPosition(index);
				buyDetailDao.save(buyDetail);
				//更新库存
				product.setCurrentQuantity(product.getCurrentQuantity()+buyDetail.getQuantity());
				
				index++;
			}
		}
		if(StringUtil.isNotNull(deleteids)){
			String[] deleteidArray = deleteids.split(SPLIT_REGEX);
			//删除
			for(String deleteid : deleteidArray){
				if("0".equals(deleteid)) continue;
				BuyDetail buyDetail = buyDetailDao.get(Integer.parseInt(deleteid));
				Product oldProduct = buyDetail.getProduct();
				//先更新之前的
				oldProduct.setCurrentQuantity(oldProduct.getCurrentQuantity()-buyDetail.getQuantity());
				buyDetailDao.delete(buyDetail);
			}
		}
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public ResponseMsgUtil deleteBuy(Buy buy) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		buy = getEntity(buy.getBuyID());
		List<BuyDetail> list = buy.getBuyDetailList();
		for(BuyDetail buyDetail : list){
			Product product = buyDetail.getProduct();
			//先更新之前的
			product.setCurrentQuantity(product.getCurrentQuantity()-buyDetail.getQuantity());
			buyDetailDao.delete(buyDetail);
		}
		deleteEntity(buy);
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public String queryBuyDetail(Buy buy) {
		buy = getEntity(buy.getBuyID());
		List<BuyDetail> list = buy.getBuyDetailList();
		
		String[] propertiesName = {"buyDetailID","product.productID","quantity","price","amount","note","product.productName",
				 "product.productSize","product.unitName"};
		String jsonArray = ListAndJsonUtil.getJSONString(list, propertiesName);
		return jsonArray;
	}

	public String getBuy(Buy buy) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);
		buy = getEntity(buy.getBuyID());
		msgUtil.setProperty("buyID", buy.getBuyID());
		msgUtil.setProperty("buyCode", buy.getBuyCode());
		msgUtil.setProperty("supplierID", buy.getSupplier().getSupplierID());
		msgUtil.setProperty("supplierName", buy.getSupplierName());
		msgUtil.setProperty("buyer", buy.getBuyer());
		msgUtil.setProperty("buyDate",DateUtils.getDateString(buy.getBuyDate()));
		msgUtil.setProperty("storeHouseID", buy.getStoreHouse().getDataDictionaryID());
		msgUtil.setProperty("note", buy.getNote());
		return msgUtil.toJSON();
	}

}
